package com.project.bookstore.common.utils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.project.bookstore.book.entity.Book;
import com.project.bookstore.category.domain.Category;
import com.project.bookstore.category.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetBookDetailFromHtml {
    @Autowired
    private CategoryMapper categoryMapper;

    void getBooks() throws IOException {
        String classpath = ResourceUtils.getURL("classpath:").getPath();
        System.out.println(classpath);
        File file = new File(classpath + "categoryInHtml.text");
        StringBuilder stringBuilder;
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        stringBuilder = new StringBuilder();
        bufferedReader.lines().forEach(e -> {
            stringBuilder.append(e.trim());
        });
        bufferedReader.close();
        System.out.println(stringBuilder);
        String re = "<div class=\"detail-img\">.*?\" src=\"(?<picture>\\S*)\" " +
                "alt=\"(?<bookName>\\S*)\".*?>(?<author>\\S*)</a><span> 著</span>" +
                ".*?>出版社: <.*?<span.*?>(?<press>\\S*)</span>" +
                ".*?>版次: <.*?<span.*?>(?<edition>\\S*)</span>" +
                ".*?>ISBN: <.*?<span.*?>(?<ISBN>\\S*)</span>" +
                ".*?>定价: <.*?<span.*?>(?<price>\\S*)</span>" +
                ".*?>装帧: <.*?<span.*?>(?<degree>\\S*)</span>" +
                ".*?>开本: <.*?<span.*?>(?<folio>\\d*)\\S*</span>" +
                ".*?>纸张: <.*?<span.*?>(?<paper>\\S*)\\S*</span>" +
                ".*?>分类: <.*?<span.*?>(?<classify>\\S*)</span>" +
                ".*?<h5.*?>内容简介: </h5>\\s*(?<info>\\S*)\\s*</li>";
        Pattern compile = Pattern.compile(re);
        Matcher matcher = compile.matcher(stringBuilder.toString());
        Book book = new Book();
        while (matcher.find()) {
            book.setPicture(matcher.group("picture"))
                    .setBookName(matcher.group("bookName"))
                    .setAuthor(matcher.group("author"))
                    .setPress(matcher.group("press"))
                    .setEdition(Integer.parseInt(matcher.group("edition")))
                    .setIsbn(matcher.group("ISBN"))
                    .setPrice(BigDecimal.valueOf(Double.parseDouble(matcher.group("price"))))
                    .setDegree(matcher.group("degree"))
                    .setFolio(Integer.parseInt(matcher.group("folio")))
                    .setPaper(matcher.group("paper"))
                    .setClassify(matcher.group("classify"))
                    .setInfo(matcher.group("info").trim());
        }
        System.out.println("book = " + book);
                    /*(id, classify, classify_id, book_name, ISBN, author, degree,
                         edition,press, price, old_price,
                         PCS,info, sale_count, picture,
                            folio, paper, is_enable, is_marketable, created, modified)*/
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", book.getClassify());
        List<Category> categories = categoryMapper.selectByMap(map);
        Long categoryId = categories.get((int) Math.random() * categories.size()).getId();
        String sql = "(null, '" + book.getClassify() + "', " + categoryId + "," +
                " '" + book.getBookName() + "', '" + book.getIsbn() + "', '" + book.getAuthor() + "', " +
                "'" + book.getDegree() + "',1,'" + book.getPress() + "'," + book.getPrice() + "," +
                " " + book.getPrice().multiply(BigDecimal.valueOf(1.2)) + ", 0,'" + book.getInfo() + "',0," +
                " '" + book.getPicture() + "'," + book.getFolio() + ",'" + book.getPaper() + "',1, 1,'2021-4-15 1:31:00', '2021-4-17 1:31:00'),\n";
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(classpath + "category.sql", true));
        bufferedWriter.write(sql);
        bufferedWriter.close();
        System.out.println("sql = " + sql);
    }

}
